//
//  MultistageTableView.m
//  NomalTest
//
//  Created by 陈小东 on 15/6/19.
//  Copyright (c) 2015年 &#26477;&#24030;&#26102;&#25237;&#20449;&#24687;&#31185;&#25216;@&#38472;&#23567;&#19996;. All rights reserved.
//

#import "MultistageTableView.h"
#import <UtilsMacro.h>
#import <UIView+Layout.h>

@interface MultistageTableView ()

@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,unsafe_unretained) NSInteger expansion;//标记展开section  default is -1

@end

static NSString *kMultistageCellIndentifier = @"kMultistageCellIndentifier";
static NSString *kMultistageKidCellIndentifier = @"kMultistageKidCellIndentifier";

@implementation MultistageTableView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"MultistageTableView";
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    self.dataArray = @[@[@"01",@"02",@"03"],@[@"10",@"11",@"12",@"13"],@[@"20",@"21",@"22",@"23",@"24"]];
    self.expansion = -1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.expansion == section){
        return ((NSArray *)self.dataArray[section]).count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    if(0 == indexPath.row){
        cell = [tableView dequeueReusableCellWithIdentifier:kMultistageCellIndentifier];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMultistageCellIndentifier];
            UIImageView *indicatorImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 5)];
            indicatorImgView.image = [UIImage imageNamed:@"expandable"];
            cell.accessoryView = indicatorImgView;
        }
        
        if(self.expansion == indexPath.section){//关闭
            cell.accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
        }else{//展开
            cell.accessoryView.transform = CGAffineTransformMakeRotation(0);
        }
        
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:kMultistageKidCellIndentifier];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMultistageKidCellIndentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    cell.textLabel.text = kFormat(@"%@", self.dataArray[indexPath.section][indexPath.row]);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(0 == indexPath.row){
        return 50.f;
    }
    return 40.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(0 == indexPath.row){//0
        if(self.expansion == indexPath.section){//关闭
            self.expansion = -1;
        }else{//展开
            self.expansion = indexPath.section;
        }
        [tableView reloadData];
    }else{
        
    }
}

@end
